Skip to contentMethod: createStatement(Resource, IRI, Value, IRI)
1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.sesame;
16:
17: import cz.cvut.kbss.ontodriver.descriptor.AxiomValueDescriptor;
18: import cz.cvut.kbss.ontodriver.model.Assertion;
19: import cz.cvut.kbss.ontodriver.model.NamedResource;
20: import cz.cvut.kbss.ontodriver.model.Value;
21: import cz.cvut.kbss.ontodriver.sesame.connector.Connector;
22: import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
23: import cz.cvut.kbss.ontodriver.sesame.util.SesameUtils;
24: import org.eclipse.rdf4j.model.Resource;
25: import org.eclipse.rdf4j.model.Statement;
26: import org.eclipse.rdf4j.model.ValueFactory;
27:
28: import java.net.URI;
29: import java.util.*;
30:
31: class AxiomSaver {
32:
33: private final Connector connector;
34: private final ValueFactory valueFactory;
35: private final SesameValueConverter valueConverter;
36:
37: AxiomSaver(Connector connector, ValueFactory valueFactory, String language) {
38: this.connector = connector;
39: this.valueFactory = valueFactory;
40: this.valueConverter = new SesameValueConverter(valueFactory, language);
41: }
42:
43: void persistAxioms(AxiomValueDescriptor axiomDescriptor) throws SesameDriverException {
44: final List<Statement> statements = new ArrayList<>();
45: for (Assertion assertion : axiomDescriptor.getAssertions()) {
46: statements.addAll(createSesameStatements(axiomDescriptor.getSubject(), assertion,
47: axiomDescriptor.getAssertionValues(assertion),
48: axiomDescriptor.getAssertionContext(assertion)));
49: }
50: if (!statements.isEmpty()) {
51: connector.addStatements(statements);
52: }
53: }
54:
55: void persistAxioms(NamedResource subject, Map<Assertion, Set<Value<?>>> values, URI context)
56: throws SesameDriverException {
57: final List<Statement> statements = new ArrayList<>();
58: for (Map.Entry<Assertion, Set<Value<?>>> entry : values.entrySet()) {
59: statements.addAll(createSesameStatements(subject, entry.getKey(), entry.getValue(), context));
60: }
61: if (!statements.isEmpty()) {
62: connector.addStatements(statements);
63: }
64: }
65:
66: private Collection<? extends Statement> createSesameStatements(NamedResource subject,
67: Assertion assertion,
68: Collection<Value<?>> assertionValues,
69: URI assertionContext)
70: throws SesameDriverException {
71: final List<Statement> statements = new ArrayList<>(assertionValues.size());
72:
73: final Resource subjectUri = SesameUtils.toSesameIri(subject.getIdentifier(), valueFactory);
74: final org.eclipse.rdf4j.model.IRI property = SesameUtils.toSesameIri(assertion.getIdentifier(),
75: valueFactory);
76: final org.eclipse.rdf4j.model.IRI context =
77: assertionContext != null ? SesameUtils.toSesameIri(assertionContext, valueFactory) : null;
78: for (Value<?> val : assertionValues) {
79: if (val == Value.nullValue()) {
80: continue;
81: }
82: org.eclipse.rdf4j.model.Value value = valueConverter.toSesameValue(assertion, val);
83: statements.add(createStatement(subjectUri, property, value, context));
84: }
85: return statements;
86: }
87:
88: private Statement createStatement(Resource subject, org.eclipse.rdf4j.model.IRI property,
89: org.eclipse.rdf4j.model.Value value, org.eclipse.rdf4j.model.IRI context) {
90:• if (context != null) {
91: return valueFactory.createStatement(subject, property, value, context);
92: } else {
93: return valueFactory.createStatement(subject, property, value);
94: }
95: }
96: }